package com.jyskkebank.android.toggl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.jyskebank.android.toggl.domain.Login;
import com.jyskebank.android.toggl.domain.Project;
import com.jyskebank.android.toggl.domain.TimeEntry;

public class TogglService {
	TogglUtil toggl;
	public Login getLoginData(String userName,String password) throws IOException{
		this.toggl = new TogglUtil();
		String in = toggl.fetchLoginData(userName, password);
		Login loginData = new Login();
		JSONObject obj;
		try {
			obj = new JSONObject(in);			
			JSONObject obj1 = (JSONObject)obj.get("data");
			loginData.setFullName((String)obj1.get("fullname"));
			loginData.setUpdatedAt((String)obj1.get("updated_at"));
			loginData.setStoreStartAndStopTime((Boolean)obj1.get("store_start_and_stop_time"));
			loginData.setJqueryDateFormat((String)obj1.get("jquery_date_format"));
			loginData.setEmail((String)obj1.get("email"));
			loginData.setJqueryTimeofdayFormat((String)obj1.get("jquery_timeofday_format"));
			loginData.setApiToken((String)obj1.get("api_token"));
			loginData.setBeginningOfWeek((Integer)obj1.get("beginning_of_week"));
			loginData.setDefaultWorkspaceId((Integer)obj1.get("default_workspace_id"));
			loginData.setNewTimeEntriesStartAutomatically((Boolean)obj1.get("new_time_entries_start_automatically"));
			loginData.setTimeofdayFormat((String)obj1.get("timeofday_format"));
			loginData.setTimeEntryRetentionDays((Integer)obj1.get("time_entry_retention_days"));
			loginData.setDateFormat((String)obj1.get("date_format"));
			loginData.setFullName((String)obj1.get("fullname"));
			loginData.setLanguage((String)obj1.get("language"));
			loginData.setId((Integer)obj1.get("id"));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return loginData;
	}
	
	public List<TimeEntry> getTimeEntries(String apiToken) {
		this.toggl = new TogglUtil();
		List<TimeEntry> timeEntryList = new ArrayList<TimeEntry>();
		String in = null;
		try {
			in = toggl.fetchTimeEntries(apiToken);			
			JSONObject obj = new JSONObject(in);
			JSONArray obj1 = (JSONArray)obj.get("data");
			System.out.println("obj1 " + obj1);
			for(int i = 0 ;i<obj1.length();i++){
				JSONObject obj2 = (JSONObject)obj1.get(i);
				TimeEntry timeEntry = new TimeEntry();
				timeEntry.setId(obj2.getLong("id"));
				timeEntry.setStopTime(obj2.getString("stop"));
				Project project = new Project();
				
				JSONObject obj3 = (JSONObject)obj2.getJSONObject("project");
				project.setClientProjectName(obj3.getString("client_project_name"));
				project.setId(obj3.getLong("id"));
				project.setName(obj3.getString("name"));
				
				timeEntry.setProject(project);
				timeEntry.setDuration(obj2.getInt("duration"));
				timeEntry.setUpdatedAt(obj2.getString("updated_at"));
				timeEntry.setIgnoreStartAndStop(obj2.getString("ignore_start_and_stop"));
				String startTime = obj2.getString("start");
				timeEntry.setStartTime(startTime.substring(0,startTime.indexOf("T")));
				
				JSONObject obj4 = (JSONObject)obj2.getJSONObject("workspace");
				timeEntry.setWorkspaceName(obj4.getString("name"));
				timeEntry.setWorkspaceId(obj4.getLong("id"));
				
				timeEntry.setDescription(obj2.getString("description"));
				timeEntry.setTagNames(obj2.getString("tag_names"));
				timeEntry.setBillabe(obj2.getBoolean("billable"));
				timeEntryList.add(timeEntry);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return timeEntryList;
	}
	
	public List<Project> getProjectList(String apiToken){
		this.toggl = new TogglUtil();
		List<Project> projectList = new ArrayList<Project>();
		String in = null; 
		try{
			in =toggl.fetchProjects(apiToken);	
			JSONObject obj = new JSONObject(in);
			JSONArray obj1 = (JSONArray)obj.get("data");
			for(int i = 0 ;i<obj1.length();i++){
				JSONObject obj2 = (JSONObject)obj1.get(i);
				Project project = new Project();
				project.setClientProjectName(obj2.getString("client_project_name"));
				project.setName(obj2.getString("name"));
				project.setId(obj2.getLong("id"));
				projectList.add(project);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return projectList;
	}
}
